﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace game
{
    public class FpsMeter : DrawableGameComponent
    {
        private static int frames;
        private static int framesPerSecond = 1;
        private static double elapsedTimeSec;

        public FpsMeter(Game game)
            : base(game)
        {
        }
      
        public override void Update(GameTime gameTime)
        {
            elapsedTimeSec += gameTime.ElapsedRealTime.TotalSeconds;

            if (elapsedTimeSec > 1.0)                                   // Basic method for measuring FPS, increment frame count until a second has elapsed.
            {
                framesPerSecond = frames;
                frames = 0;
                elapsedTimeSec = 0.0;
            }
            else
            {
                ++frames;
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Text2D.Draw(framesPerSecond.ToString(),
                new Vector2(0, 0), Color.White, new Vector2(2, 2));
            base.Draw(gameTime);
        }

    }
}
